<template>
  <div class="rightBodyContent">
    <div class="rightBodyContent-in">
      <div class="tab-body mb-10">
        <!--        <div class="tab-box">-->
        <!--          // 0待审核1通过2驳回3借出4归还-->
        <!--          <el-radio-group v-model="radioStatus" size="small" @change="changeFn()">-->
        <!--            <el-radio-button label="all">全部({{ number ? number.all : 0 }})</el-radio-button>-->
        <!--            <el-radio-button :label="1">待借出({{ number ? number.apply : 0 }})</el-radio-button>-->
        <!--            <el-radio-button :label="3">已借出({{ number ? number.lend : 0 }})</el-radio-button>-->
        <!--            <el-radio-button :label="4">已归还({{ number ? number.end : 0 }})</el-radio-button>-->
        <!--          </el-radio-group>-->
        <!--        </div>-->
        <div class="tab-box-list">
          <el-select v-model="editForm.status" placeholder="选择状态" size="small" style="width: 140px" class="mr-10">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in statusList"
                       :key="index"></el-option>
          </el-select>
          <el-input v-model="editForm.name" auto-complete="off" placeholder="填写证照名称" size="small"
                    class="w-140"></el-input>
          <el-button size="small" type="primary" plain class="ml-10" @click="search('search')">搜索</el-button>
          <el-button size="small" plain @click="search('resct')">重置</el-button>
        </div>
      </div>
      <!--本体列表，数据 -->
      <div class="table-body">
        <div class="table-body-box">
          <el-table
              ref="multipleTable"
              :data="arryList"
              :stripe="tableStyle.tableStripe"
              :border="tableStyle.tableBorder"
              tooltip-effect="dark"
              header-cell-class-name="table-text-c"
              style="width: 100%">
            <el-table-column
                prop="name"
                label="证照名称"
                width="120"
                align="center">
              <template slot-scope="scope">
                {{ scope.row.licence.name }}
              </template>
            </el-table-column>
            <el-table-column
                label="当前持有人"
                align="center"
                :show-overflow-tooltip="true">
              <template slot-scope="scope">
                {{ scope.row.licence.inuser ? scope.row.licence.inuser.realname : '--' }}
              </template>
            </el-table-column>
            <el-table-column
                label="申请人"
                align="center"
                :show-overflow-tooltip="true">
              <template slot-scope="scope">
                {{ scope.row.user ? scope.row.user.realname : '--' }}
              </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                label="申请时间"
                align="center"
                width="160">
            </el-table-column>
            <el-table-column
                prop="purpose"
                label="用途"
                align="center">
            </el-table-column>
            <el-table-column
                prop="time"
                label="预借用日期"
                align="center">
            </el-table-column>
            <el-table-column
                prop="return_time"
                label="预归还日期"
                align="center">
            </el-table-column>
            <el-table-column
                prop="overtime"
                label="剩余天数"
                align="center">
            </el-table-column>
            <!--            <el-table-column-->
            <!--                prop="remark"-->
            <!--                label="补充说明">-->
            <!--            </el-table-column>-->
            <el-table-column
                label="借出时间"
                align="center"
                prop="receive_time">
            </el-table-column>
            <el-table-column
                label="归还时间"
                align="center"
                prop="actual_return_time">
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
                align="center">
              <template slot-scope="scope">
                <!--                // 0待审核1通过2驳回3借出4归还-->
                <!--                申请通过 已驳回 待归还 已归还   -->
                <!--                <span v-if="scope.row.status==0">待审核</span>-->
                <!--                <span v-if="scope.row.status==1">待借出</span>-->
                <!--                <span v-if="scope.row.status==2">已驳回 <i>查看驳回理由</i></span>-->
                <!--                <span v-if="scope.row.status==3">待归还</span>-->
                <!--                <span v-if="scope.row.status==4">已归还</span>-->

                <el-tag type="warning" v-if="scope.row.status==0">待审核</el-tag>
                <el-tag v-if="scope.row.status==1">待借出</el-tag>
                <!--                <el-tag type="success" v-if="scope.row.status==1">通过</el-tag>-->
                <el-tag type="danger" v-if="scope.row.status==2">已驳回, 驳回说明:{{ scope.row.content }}</el-tag>
                <el-tag v-if="scope.row.status==3">待归还</el-tag>
                <el-tag type="success" v-if="scope.row.status==4">已归还</el-tag>
              </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                width="90"
                fixed="right">
              <template slot-scope="scope">
                <el-button plain size="small" @click="detailId=scope.row.id,detailUsevisible=true">
                  详情
                </el-button>
                <!--                <el-button plain type="primary" size="small" v-if="scope.row.status==1" @click="examinBtn(1,scope.row.id)">-->
                <!--                  确认借出-->
                <!--                </el-button>-->
                <!--                <el-button size="small" @click="examinBtn(2,scope.row.id)" class="ml-10" v-if="scope.row.status==3">-->
                <!--                  确认归还-->
                <!--                </el-button>-->
                <!--                <el-button size="small" @click="examinBtn(3,scope.row.id)" class="ml-10" v-if="scope.row.status==3">-->
                <!--                  提醒归还-->
                <!--                </el-button>-->

              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <!--工具条 分页-->
      <el-col :span="24" class="toolbar">
        <el-pagination
            background
            layout="total, prev, pager, next"
            :total="page.total"
            :page-size="page.pagesize"
            @current-change="handleSizeChange">
        </el-pagination>
      </el-col>
    </div>
    <cardDetail :visible.sync="detailUsevisible" :detailId="detailId" @InitFn="InitFn" intype="use"></cardDetail>
  </div>
</template>

<script>
import {pagination, tableoption, hourTime} from '@/utils/util.js';
import cardDetail from '../components/detail'

export default {
  components: {
    cardDetail
  },
  data() {
    return {
      detailVisible: false,
      useId: 0,
      useType: '',
      radioStatus: 'all',
      detailUsevisible: false,
      detailId: 0,
      handleSelection: [],
      arryList: [], // 页面数据列表
      // 设置table样式
      tableStyle: tableoption,
      // 设置table页面分页所需
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      editForm: {
        name: '', // 	整型 	必填 	1 	- 	名称
        status: 99
      },
      number: null,
      statusList: [
        {
          id: 99,
          name: '全部'
        },
        {
          id: 1,
          name: '待借出'
        },
        {
          id: 3,
          name: '已借出'
        },
        {
          id: 4,
          name: '已归还'
        },
      ]
    }
  },
  created() {
    this.InitFn()
  },
  methods: {
    // 初始化
    InitFn() {
      // type 2 审核人员 1 借用管理
      let option_ = {p: this.page.pageindex, type: 1, name: this.editForm.name}
      if (this.editForm.status !== 99) {
        option_.status = this.editForm.status;
      }
      // <!--                // 0待审核1通过2驳回3借出4归还-->
      // <!--                申请通过 已驳回 待归还 已归还   -->
      this.$fetch(this.api.licence.apply_list, option_).then((response) => {
        if (response.code === 0) {
          // <!--                // 0待审核1通过2驳回3借出4归还-->
          // <!--                申请通过 已驳回 待归还 已归还   -->
          // <span v-if="scope.row.status==0">待审核</span>
          //   <span v-if="scope.row.status==1">待借出</span>
          // <span v-if="scope.row.status==2">已驳回 <i>查看驳回理由</i></span>
          // <span v-if="scope.row.status==3">待归还</span>
          // <span v-if="scope.row.status==4">已归还</span>
          for (let i in response.list.data) {
            if (response.list.data[i].status === 3) {
              let day1 = new Date(response.list.data[i].return_time);
              // let day1 = new Date('2021-11-25')
              let day2 = new Date(response.list.data[i].time);
              let overTime = (day2 - day1) / (1000 * 60 * 60 * 24);
              console.log('overTime:', overTime)
              if (overTime > 0) {
                let time_end = parseInt(overTime)
                response.list.data[i].overtime = time_end + '天'
              } else {
                response.list.data[i].overtime = 0 + '天'
              }
            } else {
              response.list.data[i].overtime = '--'
            }
          }

          this.arryList = JSON.parse(JSON.stringify(response.list.data))
          this.page.total = response.list.total
          this.number = response.number
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    search(option) {
      this.page.pageindex = 1;
      if (option !== 'search') {
        this.editForm.name = '';
      }
      this.InitFn()
    },
    examinBtn(type, id) {
      // 1 确认借出 2 提醒归还 3 确认归还
      let that = this;
      if (type === 1) {
        this.$confirm('确认借出证照, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          that.lendLicence(id)
        }).catch(() => {
        });
      } else if (type === 2) {
        // 推送归还 提醒
        this.useType = type;
        this.useId = id;
        this.detailVisible = true
      } else if (type === 3) {
        // 提醒归还
        this.remindLicence(id)
      }
    },
    changeFn() {
      this.InitFn()
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageindex = val
      this.InitFn()
    },
    // 搜索组件
    searchFn(option) {
      this.page.pageindex = 1
      this.searchForm = option
      this.InitFn()
    },
    // 借出
    async lendLicence(id) {
      let data = await this.$post(this.api.licence.receive, {id: id});
      console.log('借出成功：', data)
      if (!data.code) {
        this.InitFn()
      }
    },
    // 提醒归还
    async remindLicence(id) {
      let data = await this.$post(this.api.licence.remind, {id: id});
      console.log('提醒成功：', data)
      if (!data.code) {
        this.$message.success('提醒成功！')
        this.InitFn()
      }
    },
    subimitExamin(type, id, value) {
      let n = this;
      let option = {id: id, type: type}
      if (type === 2) {
        option.content = value;
      }
      this.$post(this.api.licence.examine, option).then((response) => {
        if (response.code === 0) {
          n.InitFn()
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    }
  },
  mounted() {
  },
  // 缓存机制 end
}
</script>

<style scoped>

.wx-user-box-line img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin-right: 5px;
}

.wx-user-box-line {
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-wx {
  cursor: pointer;
  padding: 5px;
}

.btn-wx:hover {
  color: #1b8de5;
  text-decoration: underline;
}

.delet-wx {
  color: red;
  cursor: pointer;
}

.delet-wx:hover {
  text-decoration: underline;
}

.tab-body {
  display: flex;
  align-items: center;
}

.tab-box {
  flex: 1;
}

.tab-box-list {
  display: flex;
  align-items: center;
}
</style>
